//
//  RecUserViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-19.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecUserViewController.h"
#import "RecUserInfoTableViewCell.h"
#import "RecUserNoteModel.h"
#import "RecUserDetailsViewController.h"
#import "RecTravelNotesDetailsViewController.h"
#import "RecAchiveViewController.h"
#import "Reachability.h"
@interface RecUserViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation RecUserViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
   self.navigationController.navigationBarHidden = NO;
   // self.navigationController.navigationBar.hidden = NO;
   self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.dataArr = [[NSMutableArray alloc] init];
    [self getData];
    
}
#pragma mark - 页面创建
-(void)setUpPage
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    _table.separatorColor = [UIColor clearColor];
    [self.view addSubview:_table];
    
    //背景图片
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250 * HEIGHT)];
    [image sd_setImageWithURL:[NSURL URLWithString:self.imageStr] placeholderImage:PLACEHOLDERIMAGE];
    _table.tableHeaderView = image;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250 * HEIGHT)];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.5;
    [image addSubview:view];

    
    //头像
    UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(160 * WIDTH, 80 * HEIGHT, 60 * WIDTH, 60 * WIDTH)];
    [iconImage sd_setImageWithURL:[NSURL URLWithString:self.iconStr] placeholderImage:PLACEHOLDERIMAGE];
    iconImage.layer.cornerRadius = 30 * WIDTH;
    iconImage.layer.masksToBounds = YES;
    [image addSubview:iconImage];
    
    //关注和粉丝
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(75 * WIDTH, 150 * HEIGHT, SCREEN_WIDTH - 150 * WIDTH, 40 * HEIGHT)];
    infoLabel.font = [UIFont boldSystemFontOfSize:18.];
    infoLabel.text = [NSString stringWithFormat:@"关注 %@ / 粉丝 %@",self.following_count.stringValue, self.followingUser.stringValue];
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.textColor = [UIColor whiteColor];
    [image addSubview:infoLabel];
    
    UIImageView *levelImage = [[UIImageView alloc] initWithFrame:CGRectMake(150 * WIDTH, 200 * HEIGHT, 20 * WIDTH, 20 * HEIGHT)];
    [levelImage setImage:[UIImage imageNamed:@"level.png"]];
    [image addSubview:levelImage];
    
    //星级
    UILabel *levelLabel = [[UILabel alloc] initWithFrame:CGRectMake(160 * WIDTH, 195 * HEIGHT, SCREEN_WIDTH - 300 * WIDTH, 30 * HEIGHT)];
    levelLabel.font = [UIFont systemFontOfSize:14.];
    levelLabel.alpha = 0.7;
    levelLabel.textAlignment  = NSTextAlignmentCenter;
    levelLabel.textColor = [UIColor whiteColor];
    levelLabel.text = [NSString stringWithFormat:@"Level %@",self.levelStr];
    [image addSubview:levelLabel];
    
    image.userInteractionEnabled = YES;
    
    //成就按钮
    UIButton *achiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    achiveButton.frame = CGRectMake(260 * WIDTH, 190 * HEIGHT, 50 * WIDTH, 40 * HEIGHT);
    achiveButton.layer.borderColor = [[UIColor whiteColor]CGColor];
    achiveButton.layer.borderWidth = 2;
    achiveButton.layer.cornerRadius = 10;
    achiveButton.layer.masksToBounds = YES;
    [achiveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [achiveButton setTitle:@"成就" forState:UIControlStateNormal];
    [achiveButton addTarget:self action:@selector(toAchive) forControlEvents:UIControlEventTouchUpInside];
    [image addSubview:achiveButton];
    
    //名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH, 10 * HEIGHT, SCREEN_WIDTH - 100 * WIDTH, 50 * HEIGHT)];
    nameLabel.font = [UIFont boldSystemFontOfSize:22.];
    nameLabel.text = self.name;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.textColor = [UIColor whiteColor];
    [image addSubview:nameLabel];
    
}
-(void)toAchive
{
    RecAchiveViewController *achive = [[RecAchiveViewController alloc] init];
    [self.navigationController pushViewController:achive animated:YES];
    achive.userId = self.userId;
    achive.coverImageStr = self.imageStr;
    achive.iconImageStr = self.iconStr;
    achive.name = self.name;
}
#pragma mark - tableview Delegate & Datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190 * HEIGHT;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecUserNoteModel *model = [_dataArr objectAtIndex:indexPath.row];
    static NSString *cellID = @"cell";
    RecUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[RecUserInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell getData:model];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecUserNoteModel *model = [_dataArr objectAtIndex:indexPath.row];
    
    
    //如果是足迹
    if (![model.waypoints.stringValue isEqualToString:@"0"]) {
        RecTravelNotesDetailsViewController *travel = [[RecTravelNotesDetailsViewController alloc] init];
        [self.navigationController pushViewController:travel animated:YES];
        travel.myId = model.myId;
        travel.avatar_l = self.iconStr;
        travel.name = self.name;
     //如果是故事
    }else{
        RecUserDetailsViewController *user = [[RecUserDetailsViewController alloc] init];
        [self.navigationController pushViewController:user animated:YES];
        user.myID = model.myId;
        user.name = model.name;
        user.coverImage = model.cover_image;
        
    }

}

#pragma mark - 数据获取
-(void)getData
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/v2/users/%@/",self.userId] block:^(id block) {
        
        NSDictionary *dic = [[block objectForKey:@"data"]objectForKey:@"user_info"];
        self.imageStr = [dic objectForKey:@"cover"];
        self.iconStr = [dic objectForKey:@"avatar_l"];
        self.name = [dic objectForKey:@"name"];
        self.following_count = [dic objectForKey:@"followings_count"];
        self.followingUser = [dic objectForKey:@"followers_count"];
        self.levelStr = [[[dic objectForKey:@"experience"]objectForKey:@"level_info"]objectForKey:@"value"];
        
        for (NSDictionary *dic in [[block objectForKey:@"data"]objectForKey:@"trips"]) {
            RecUserNoteModel *model = [RecUserNoteModel modelWithDictionary:dic];
            [self.dataArr addObject:model];
        }
        
        [self setUpPage];
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
